;-------------------------------------------------------------LICENSE--------------------------------------------------------------;
;                                                                                                                                  ;
;The MAP code is written in Fortran language for magnetohydrodynamics (MHD) calculation with the adaptive mesh refinement (AMR)    ;
;and Message Passing Interface (MPI) parallelization.                                                                              ;
;                                                                                                                                  ;
;Copyright (C) 2012                                                                                                                ;
;Ronglin Jiang                                                                                                                     ;
;rljiang@ssc.net.cn                                                                                                                ;
;585 Guoshoujing Road. Pudong, Shanghai, P.R.C. 201203                                                                             ;
;                                                                                                                                  ;
;This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License         ;
;as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.             ;
;                                                                                                                                  ;
;This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of    ;
;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.                        ;
;                                                                                                                                  ;
;You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software     ;
;Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.                                                   ;
;                                                                                                                                  ;
;-------------------------------------------------------------LICENSE--------------------------------------------------------------;

pro rddt_map_3d, x_data, y_data, z_data, var_data, datavis_path, info_name, var_id, t_id

;   info_name = 'info.mapvis'
;   datavis_path = '/home/rljiang/TVD-MMC/MHD-3d-DC-AMR-MPI/Blast_Wave/datavis'
;   var_id = 2
;   t_id = 0

   on_error, 2

   rdpr_map_3d, par, datavis_path, info_name

   cd, datavis_path, current = old_dir

   lv = 0l & nc = 0l
   dx = 0.0d0 & dy = 0.0d0 & dz = 0.0d0
   x = dblarr(par.nx) & y = dblarr(par.ny) & z = dblarr(par.nz)
   if par.output_real8_flag eq 4 then begin
      var_signle = fltarr(par.nx, par.ny, par.nz)
      var = fltarr(par.nx, par.ny, par.nz, par.var_dim[var_id])
   endif else begin
      var_signle = dblarr(par.nx, par.ny, par.nz)
      var = dblarr(par.nx, par.ny, par.nz, par.var_dim[var_id])
   endelse

   block_len_front = 0ll
   for i_var = 0, var_id - 1 do block_len_front = block_len_front +                                                                $
      par.nx * par.ny * par.nz * par.var_dim[i_var] * par.output_real8_flag * 1ll + 8ll

   block_len_behind = 0ll
   for i_var = 0, par.n_var - 1 do block_len_behind = block_len_behind +                                                           $
      par.nx * par.ny * par.nz * par.var_dim[i_var] * par.output_real8_flag * 1ll + 8ll
   block_len_behind = block_len_behind - block_len_front

   skip = 0ll

   endian = 0
   catch, bad_endian

   if bad_endian ne 0 then begin
      print, '|----Swap endian----|'
      endian = 1
      close, /all
   endif

   for i_t = t_id, t_id do begin
      print, 'Read the time:', par.t[i_t]
      print, 'Read the variables:', par.var_name[var_id]
      filename = 'data' + name_seq(strcompress(i_t, /remove_all), '4.0', /nosymbol) + '.mapvis'

      openr, unit, filename, /get_lun, swap_endian = endian, /f77_unformatted
      for i_block = 0, par.n_blocks_all[i_t] - 1 do begin
         point_lun, unit, skip
         readu, unit, lv, nc
         skip = skip + 4ll + 4ll + 8ll

         point_lun, unit, skip
         readu, unit, dx, dy, dz
         skip = skip + 8ll + 8ll + 8ll + 8ll

         point_lun, unit, skip
         readu, unit, x
         x_data[*, i_block] = x
         skip = skip + 8ll * par.nx + 8ll

         point_lun, unit, skip
         readu, unit, y
         y_data[*, i_block] = y
         skip = skip + 8ll * par.ny + 8ll

         point_lun, unit, skip
         readu, unit, z
         z_data[*, i_block] = z
         skip = skip + 8ll * par.nz + 8ll

         skip = skip + block_len_front
         point_lun, unit, skip
         readu, unit, var
         var_data[*, *, *, i_block] = var
         skip = skip + block_len_behind
      endfor
      close, unit
      free_lun, unit
   endfor

   catch, /cancel

   cd, old_dir

end

